<html>
<head><meta charset="utf-8"><title>how to decide whether an instance is a method? · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/how.20to.20decide.20whether.20an.20instance.20is.20a.20method.3F.html">how to decide whether an instance is a method?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="207174331"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/how%20to%20decide%20whether%20an%20instance%20is%20a%20method%3F/near/207174331" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jason Hu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/how.20to.20decide.20whether.20an.20instance.20is.20a.20method.3F.html#207174331">(Aug 17 2020 at 17:44)</a>:</h4>
<p>Hi,</p>
<p>given an instance (mono item if that matters), how do I decide whether it is a method call, and if so, how do I get the type of <code>self</code> and the type the impl block is for?</p>



<a name="207181338"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/how%20to%20decide%20whether%20an%20instance%20is%20a%20method%3F/near/207181338" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/how.20to.20decide.20whether.20an.20instance.20is.20a.20method.3F.html#207181338">(Aug 17 2020 at 18:44)</a>:</h4>
<p><span class="user-mention" data-user-id="320010">@Jason Hu</span> What do you need it for?</p>



<a name="207268020"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/how%20to%20decide%20whether%20an%20instance%20is%20a%20method%3F/near/207268020" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jason Hu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/how.20to.20decide.20whether.20an.20instance.20is.20a.20method.3F.html#207268020">(Aug 18 2020 at 14:44)</a>:</h4>
<p><span class="user-mention" data-user-id="133247">@bjorn3</span> that is what I want to do. in the project I am working on I want to do some inspection and distinguish normal functions from methods.</p>



<a name="207280107"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/how%20to%20decide%20whether%20an%20instance%20is%20a%20method%3F/near/207280107" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/how.20to.20decide.20whether.20an.20instance.20is.20a.20method.3F.html#207280107">(Aug 18 2020 at 16:13)</a>:</h4>
<p>You could look at the <code>DefPath</code> associated with the <code>DefId</code> of the instance (<code>tcx.def_path</code>) The second to last item in <code>DefPath.data</code> will be <code>DisambiguatedDefPathData { data: DefPathData::Impl, disambiguator: _ }</code> if it is a method. You can probably get the <code>DefId</code> of the impl block using <code>tcx.definitions.def_key</code>. I don't really know how to then get to the impl type. For the self type you can use <code>tcx.generics_of</code> on the instance <code>DefId</code>. Through this you should be able to get the index of the <code>Self</code> param. You can then get that param from <code>instance.substs</code>.</p>



<a name="207281791"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/how%20to%20decide%20whether%20an%20instance%20is%20a%20method%3F/near/207281791" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jason Hu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/how.20to.20decide.20whether.20an.20instance.20is.20a.20method.3F.html#207281791">(Aug 18 2020 at 16:27)</a>:</h4>
<p>what if I just consider the current crate? would that be better? in the <code>Impl</code> case of HIR, there is a <code>self_ty</code> field. If I have a HIR type, can I convert that to a <code>ty::Ty</code>? since I have reached MIR, can I assume this conversion must succeed?</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>